<template>
  <j-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row class="form-row" :gutter="{ xs: 8, sm: 16, md: 24, lg: 32 }">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="会员id">
              <a-input placeholder="请输入会员id" v-decorator="['memberId', validatorRules.memberId ]"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="督导的会员id">
              <a-input placeholder="请输入督导的会员id" v-decorator="['supervisorMemberId', {}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="24">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="分销员状态">
              <a-input-number v-decorator="[ 'state', {}]"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="分销员等级">
              <a-input-number v-decorator="[ 'levelId', {}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="24">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="等级开始日期">
              <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'startTime', {}]"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="预计结束日期">
              <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'expirationTime', {}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="24">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="新增成长值">
              <a-input-number v-decorator="[ 'incrementGrowthValue', {}]"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="期初剩余成长值">
              <a-input-number v-decorator="[ 'openingGrowthValue', {}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="24">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="期初总成长值">
              <a-input-number v-decorator="[ 'openingTotalGrowthValue', {}]"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="发票方式">
              <a-input-number v-decorator="[ 'invoiceSupplyType', {}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="24">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="last结算时间点">
              <a-date-picker showTime format='YYYY-MM-DD HH:mm:ss' v-decorator="[ 'lastSettlementTime', {}]"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="结算期初结入金">
              <a-input-number v-decorator="[ 'settlementOpeningIncomeAmount', {}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="24">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="结算期初可用金">
              <a-input-number v-decorator="[ 'settlementOpeningAvailableAmount', {}]"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="结算期初已提金">
              <a-input-number v-decorator="[ 'settlementOpeningWithdrawnAmount', {}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="24">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="结算期初已抵用金">
              <a-input-number v-decorator="[ 'settlementOpeningOutgoingsAmount', {}]"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="结算期内结入金">
              <a-input-number v-decorator="[ 'settlementWithinIncomeAmount', {}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="24">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="结算期内已提金">
              <a-input-number v-decorator="[ 'settlementWithinWithdrawnAmount', {}]"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="结算期内已抵用金">
              <a-input-number v-decorator="[ 'settlementWithinOutgoingsAmount', {}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row class="form-row" :gutter="24">
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="可用金额">
              <a-input-number v-decorator="[ 'availableAmount', {}]"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              style="margin-right: 25px"
              label="提现中金额">
              <a-input-number v-decorator="[ 'withdrawnAmount', {}]"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from 'moment'

  export default {
    name: 'DistAccountModal',
    data() {
      return {
        title: '操作',
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {
          memberId: { rules: [{ required: true, message: '请输入会员id!' }] }
        },
        url: {
          add: '/fenxiao/distAccount/add',
          edit: '/fenxiao/distAccount/edit'
        }
      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({})
      },
      edit(record) {
        this.form.resetFields()
        this.model = Object.assign({}, record)
        this.visible = true
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'memberId', 'supervisorMemberId', 'state', 'levelId', 'incrementGrowthValue', 'openingGrowthValue', 'openingTotalGrowthValue', 'invoiceSupplyType', 'settlementOpeningIncomeAmount', 'settlementOpeningAvailableAmount', 'settlementOpeningWithdrawnAmount', 'settlementOpeningOutgoingsAmount', 'settlementWithinIncomeAmount', 'settlementWithinWithdrawnAmount', 'settlementWithinOutgoingsAmount', 'availableAmount', 'withdrawnAmount'))
          //时间格式化
          this.form.setFieldsValue({ startTime: this.model.startTime ? moment(this.model.startTime) : null })
          this.form.setFieldsValue({ expirationTime: this.model.expirationTime ? moment(this.model.expirationTime) : null })
          this.form.setFieldsValue({ lastSettlementTime: this.model.lastSettlementTime ? moment(this.model.lastSettlementTime) : null })
        })

      },
      close() {
        this.$emit('close')
        this.visible = false
      },
      handleOk() {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let httpurl = ''
            let method = ''
            if (!this.model.id) {
              httpurl += this.url.add
              method = 'post'
            } else {
              httpurl += this.url.edit
              method = 'put'
            }
            let formData = Object.assign(this.model, values)
            //时间格式化
            formData.startTime = formData.startTime ? formData.startTime.format('YYYY-MM-DD HH:mm:ss') : null
            formData.expirationTime = formData.expirationTime ? formData.expirationTime.format('YYYY-MM-DD HH:mm:ss') : null
            formData.lastSettlementTime = formData.lastSettlementTime ? formData.lastSettlementTime.format('YYYY-MM-DD HH:mm:ss') : null

            console.log(formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.confirmLoading = false
              that.close()
            })


          }
        })
      },
      handleCancel() {
        this.close()
      }


    }
  }
</script>

<style lang="less" scoped>

</style>